package mc.contents.web.request;

import mc.contents.web.CrawConfig;
import mc.contents.web.SourceConfig;
import mc.contents.web.common.AbstractPostSender;
import mc.contents.web.common.SortMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 14-12-8.
 */
public class RankPostSender extends AbstractPostSender {
    private static Logger log = LoggerFactory.getLogger( RankPostSender.class );
    private RankCrawRequest crawRequest;
    private Long id;
    private CrawConfig crawConfig;

    public RankPostSender( RankCrawRequest crawRequest,CrawConfig crawConfig,String configName,Long id ) {
        super( crawConfig, id, configName );
        this.crawRequest = crawRequest;
        this.id = id;
        this.crawConfig = crawConfig;
    }

    @Override
    public List<Task> buildTaskList() {
        int offset = crawRequest.getOffset();
        String sourceType = crawRequest.getSourceType();
        String sort = crawRequest.getSortType();
        String sortType = SortMap.getSortType( sort );
        SourceConfig sourceConfig = SourceConfig.getSourceConfig( sourceType );
        String encodeKeyword = getEncodeKeyword( crawRequest.getKeyword(),sourceConfig.getCharset() );
        SimpleDateFormat format = new SimpleDateFormat( "yyyyMMdd" );

        List<Task> taskList = new ArrayList<>( offset );
        for ( int i = 0; i < offset; i++ ) {
            Args args = new Args();
            args.setKeyword( encodeKeyword );
            args.setSort( sortType );
            args.setSourceType( sourceType );
            if("mTaobao".equals( sourceType ) || "mTmall".equals( sourceType ) ){
                args.setS( String.valueOf( i+1 ) );
            }else{
                args.setS( String.valueOf( i * sourceConfig.getPageSize() ) );
            }
            args.setId( String.valueOf( id ) );
            args.setCallbackUrl( crawConfig.getCallbackUrl()+"rankResponse" );
            args.setPageIndex( String.valueOf( i ) );
            if("mTaobao".equals( sourceType )){
                args.setInitiativeId( "jsonp"+(System.currentTimeMillis()) );
            }else{
                String str = format.format( new Date(  ) );
                if(sortType.equals( "renqi-desc" )){
                    str += "&bcoffset=1";
                }
                if(sortType.equals( "default" )){
                    str += "&bcoffset=-8";
                }
                args.setInitiativeId( str );
            }

            String hook;
            if("mTaobao".equals( sourceType ) || "mTmall".equals( sourceType )){
                hook = "function(args,result){return{url:args.callbackUrl+\"?id=\"+args.id+\"&pageIndex=\"+args.pageIndex+\"&sourceType=\"+args.sourceType,data:\"num_iid=\"+result.item+\"&client_ip=\"+result.client_ip};}";
            }else{
                hook = "function(args,result){return{url:args.callbackUrl+\"?id=\"+args.id+\"&pageIndex=\"+args.pageIndex+\"&sourceType=\"+args.sourceType,data:\"num_iid=\"+(result.itemId||[]).join(',')+\"&client_ip=\"+result.client_ip};}";
            }
            args.setHook(hook);
            Task task = new Task();
            task.setArgs( args );
            task.setGuest_id( String.valueOf( id ) );
            taskList.add( task );
        }
        return taskList;
    }

    private String getEncodeKeyword( String keyword, String charset ) {
        String encodeKeyword = null;
        try {
            encodeKeyword = URLEncoder.encode( keyword,charset);
        } catch ( UnsupportedEncodingException e ) {
            log.error( keyword + " encode error " );
        }
        return encodeKeyword;
    }
}
